package com.yanqu.road.dao.impl.activity.hsisland;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.hsisland.HsIslandPosition;
import com.yanqu.road.entity.activity.hsisland.data.HsIslandEventData;
import com.yanqu.road.entity.activity.hsisland.data.HsIslandMonsterData;
import com.yanqu.road.entity.activity.hsisland.data.HsIslandReceiveRelicRewardData;
import com.yanqu.road.utils.string.StringUtils;
import org.apache.mina.util.ConcurrentHashSet;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class HsIslandEventDataDaoImpl extends TempDataObjectDao<HsIslandEventData> {

    public static String getHsIslandEventDataSql = "select * from `t_c_activity_hs_island_event_data` where `activityId`=? and `groupId`=? and `unionId`=? and `floorId`=? and `islandId`=?";
    public Map<String, HsIslandEventData> getHsIslandEventMap(int activityId,int groupId,String unionId,int floorId,int islandId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            Map<String, HsIslandEventData> map = new ConcurrentHashMap<>();
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getHsIslandEventDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            call.setString(i++, unionId);
            call.setInt(i++, floorId);
            call.setInt(i++, islandId);
            rs = call.executeQuery();
            while (rs.next()) {
                HsIslandEventData data = parse(rs);
                for (HsIslandPosition position : data.getPositions()) {
                    map.put(position.getKey(), data);
                }
            }
            return map;
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
    }

    /**
     * 获取事件类型数量MAP
     */
    public static String getEventTypeNumMapSql = "select eventId, count(*) as num from `t_c_activity_hs_island_event_data` where `activityId`=? and `groupId`=? and `unionId`=? and `floorId`=? and `islandId`=? group by eventId";
    public Map<Integer, Integer> getEventTypeNumMap(int activityId, int groupId, String unionId, int floorId, int islandId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            Map<Integer, Integer> map = new ConcurrentHashMap<>();
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getEventTypeNumMapSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            call.setString(i++, unionId);
            call.setInt(i++, floorId);
            call.setInt(i++, islandId);
            rs = call.executeQuery();
            while (rs.next()) {
                map.put(rs.getInt("eventId"), rs.getInt("num"));
            }
            return map;
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
    }

    public static String getHsIslandEventDataListSql = "select * from `t_c_activity_hs_island_event_data` where `activityId`=? and `groupId`=? and `userId`=? ";

    public List<HsIslandEventData> getHsIslandEventList(int activityId, int groupId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            List<HsIslandEventData> list = new ArrayList<>();
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getHsIslandEventDataListSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                list.add(parse(rs));
            }
            return list;
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
    }


    public static String updateHsIslandEventDataSql = "update `t_c_activity_hs_island_event_data` set eventId=?,positions=?,finishFlag=?,rewardFlag=?,receiveRelicRewardMap=?,monsterData=?,monsterWinUserId=?,chooseConfigId=?,pvpUnionId=?,pvpUserIds=?,buffId=?,boxId=?,giftConfigId=?,buffIds=?,lightFlag=?,delFlag=?,createTime=? where activityId=? and groupId=? and unionId=? and floorId=? and islandId=? and userId=? and eventUnId=?";
    @Override
    protected boolean updateData(HsIslandEventData hsIslandEventData) {
        hsIslandEventData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandEventData.getEventId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(hsIslandEventData.getPositions())));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandEventData.getFinishFlag()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandEventData.getRewardFlag()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(hsIslandEventData.getReceiveRelicRewardMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(hsIslandEventData.getMonsterData())));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandEventData.getMonsterWinUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandEventData.getChooseConfigId()));
        para.put(i++, new DbParameter(Types.VARCHAR, hsIslandEventData.getPvpUnionId()));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(hsIslandEventData.getPvpUserIds(), ",")));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandEventData.getBuffId()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandEventData.getBoxId()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandEventData.getGiftConfigId()));
        String buffIdsStr = "";
        if (!hsIslandEventData.getBuffIds().isEmpty()) {
            buffIdsStr = JSON.toJSONString(hsIslandEventData.getBuffIds());
        }
        para.put(i++, new DbParameter(Types.VARCHAR, buffIdsStr));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandEventData.getLightFlag()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandEventData.getDelFlag()));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandEventData.getCreateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandEventData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandEventData.getGroupId()));
        para.put(i++, new DbParameter(Types.VARCHAR, hsIslandEventData.getUnionId()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandEventData.getFloorId()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandEventData.getIslandId()));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandEventData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandEventData.getEventUnId()));
        if (getDb().executeNoneQuery(updateHsIslandEventDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertHsIslandEventDataSql = "insert into `t_c_activity_hs_island_event_data`(activityId,groupId,unionId,floorId,islandId,userId,eventUnId,eventId,positions,finishFlag,rewardFlag,receiveRelicRewardMap,monsterData,monsterWinUserId,chooseConfigId,pvpUnionId,pvpUserIds,buffId,boxId,giftConfigId,buffIds,lightFlag,delFlag,createTime) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(HsIslandEventData hsIslandEventData) {
        hsIslandEventData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandEventData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandEventData.getGroupId()));
        para.put(i++, new DbParameter(Types.VARCHAR, hsIslandEventData.getUnionId()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandEventData.getFloorId()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandEventData.getIslandId()));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandEventData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandEventData.getEventUnId()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandEventData.getEventId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(hsIslandEventData.getPositions())));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandEventData.getFinishFlag()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandEventData.getRewardFlag()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(hsIslandEventData.getReceiveRelicRewardMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(hsIslandEventData.getMonsterData())));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandEventData.getMonsterWinUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandEventData.getChooseConfigId()));
        para.put(i++, new DbParameter(Types.VARCHAR, hsIslandEventData.getPvpUnionId()));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(hsIslandEventData.getPvpUserIds(), ",")));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandEventData.getBuffId()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandEventData.getBoxId()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandEventData.getGiftConfigId()));
        String buffIdsStr = "";
        if (!hsIslandEventData.getBuffIds().isEmpty()) {
            buffIdsStr = JSON.toJSONString(hsIslandEventData.getBuffIds());
        }
        para.put(i++, new DbParameter(Types.VARCHAR, buffIdsStr));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandEventData.getLightFlag()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandEventData.getDelFlag()));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandEventData.getCreateTime()));
        if (getDb().executeNoneQuery(insertHsIslandEventDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected HsIslandEventData parseData(ResultSet rs) throws SQLException {
        HsIslandEventData hsIslandEventData = new HsIslandEventData();
        hsIslandEventData.setActivityId(rs.getInt("activityId"));
        hsIslandEventData.setGroupId(rs.getInt("groupId"));
        hsIslandEventData.setUnionId(rs.getString("unionId"));
        hsIslandEventData.setFloorId(rs.getInt("floorId"));
        hsIslandEventData.setIslandId(rs.getInt("islandId"));
        hsIslandEventData.setUserId(rs.getLong("userId"));
        hsIslandEventData.setEventUnId(rs.getInt("eventUnId"));
        hsIslandEventData.setEventId(rs.getInt("eventId"));
        hsIslandEventData.setPositions(JSON.parseObject(rs.getString("positions"), new TypeReference<List<HsIslandPosition>>() {}));
        hsIslandEventData.setFinishFlag(rs.getInt("finishFlag"));
        hsIslandEventData.setRewardFlag(rs.getInt("rewardFlag"));
        hsIslandEventData.setReceiveRelicRewardMap(JSON.parseObject(rs.getString("receiveRelicRewardMap"), new TypeReference<Map<Integer, HsIslandReceiveRelicRewardData>>() {}));
        hsIslandEventData.setMonsterData(JSON.parseObject(rs.getString("monsterData"), HsIslandMonsterData.class));
        hsIslandEventData.setMonsterWinUserId(rs.getLong("monsterWinUserId"));
        hsIslandEventData.setChooseConfigId(rs.getInt("chooseConfigId"));
        hsIslandEventData.setPvpUnionId(rs.getString("pvpUnionId"));
        List<Long> pvpUserIds = new ArrayList<>();
        if (!StringUtils.isNullOrEmpty(rs.getString("pvpUserIds"))) {
            pvpUserIds = StringUtils.stringToLongList(rs.getString("pvpUserIds"), ",");
        }
        hsIslandEventData.setPvpUserIds(pvpUserIds);
        hsIslandEventData.setBuffId(rs.getInt("buffId"));
        hsIslandEventData.setBoxId(rs.getInt("boxId"));
        hsIslandEventData.setGiftConfigId(rs.getInt("giftConfigId"));
        hsIslandEventData.setDelFlag(rs.getInt("delFlag"));
        Set<Integer> buffIds = new ConcurrentHashSet<>();
        if (!StringUtils.isNullOrEmpty(rs.getString("buffIds"))) {
            buffIds = JSON.parseObject(rs.getString("buffIds"), new TypeReference<Set<Integer>>() {});
        }
        hsIslandEventData.setBuffIds(buffIds);
        hsIslandEventData.setLightFlag(rs.getInt("lightFlag"));
        hsIslandEventData.setCreateTime(rs.getLong("createTime"));
        return hsIslandEventData;
    }
}
